package main

import (
	"encoding/json"
	"fmt"
	flow "gitee.com/dingyangzhuang/dgoflow/flow/v1"
	goflow "gitee.com/dingyangzhuang/dgoflow/v1"
	"gitee.com/dingyangzhuang/dingutils/dlog"
	"log/slog"
	"time"
)

type JsonFlow struct {
	Ips          []string `json:"ips"`
	Cmds         []string `json:"cmds"`
	IsGet        bool     `json:"isGet"`
	GetCmd       string   `json:"getCmd"`
	DownloadPath string   `json:"downloadPath"`
}

// Workload function
func doSomething(data []byte, option map[string][]string) ([]byte, error) {
	var request = JsonFlow{}
	err := json.Unmarshal(data, &request)
	if err != nil {
		return nil, err
	}
	fmt.Printf("获取数据结果:%+v \n", request)

	time.Sleep(2 * time.Second)
	fmt.Println("执行了doSomething")
	time.Sleep(2 * time.Second)

	fmt.Printf("数据和选项是: %+v  %+v\n", data, option)
	return data, nil
}
func doSomething2(data []byte, option map[string][]string) ([]byte, error) {
	var request = JsonFlow{}
	err := json.Unmarshal(data, &request)
	if err != nil {
		return nil, err
	}
	fmt.Printf("获取数据结果:%+v \n", request)
	time.Sleep(2 * time.Second)

	fmt.Printf("数据和选项是: %+v  %+v\n", data, option)

	fmt.Println("执行了doSomething2")

	return []byte(fmt.Sprintf("you said \"%s\"", string(data))), nil
}

// Define provide definition of the workflow
func DefineWorkflow(workflow *flow.Workflow, context *flow.Context) error {
	dag := workflow.Dag()
	dag.Node("1111111111111111111", doSomething)
	dag.Node("2222222222222222222", doSomething2)
	dag.Edge("1111111111111111111", "2222222222222222222")

	return nil
}

func main() {
	dlog.SetFileJsonLogger("./logs/newapp.log", false, slog.LevelDebug, nil)
	fs := &goflow.FlowService{
		Port:              8089,
		RedisURL:          "localhost:6379",
		OpenTraceUrl:      "localhost:5775",
		WorkerConcurrency: 5,
		EnableMonitoring:  true,
		DebugEnabled:      true,
	}
	fs.Register("dingyz", DefineWorkflow)

	//cfg := jaegercfg.Configuration{
	//	Sampler: &jaegercfg.SamplerConfig{
	//		Type:  jaeger.SamplerTypeConst,
	//		Param: 1,
	//	},
	//	Reporter: &jaegercfg.ReporterConfig{
	//		LogSpans:           true,
	//		LocalAgentHostPort: "10.114.102.164:6831",
	//	},
	//	ServiceName: "dingyz",
	//}

	//tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaegerlog.StdLogger))
	//if err != nil {
	//	panic(any(err))
	//}
	//defer closer.Close()
	//单个追踪
	//single_span := tracer.StartSpan("single-span")
	//time.Sleep(time.Second * 3)
	//single_span.Finish()

	////父子追踪
	//
	//parentSpan := tracer.StartSpan("main")
	//
	//span := tracer.StartSpan("test", opentracing.ChildOf(parentSpan.Context()))
	//time.Sleep(time.Second)
	//span.Finish()
	//span2 := tracer.StartSpan("test2", opentracing.ChildOf(span.Context()))
	//time.Sleep(time.Second * 3)
	//span2.Finish()
	//
	//parentSpan.Finish()

	//fmt.Println("执行1")
	//
	//fmt.Println("开启worker")
	fs.Start()

	//fmt.Println("执行=----------")
	//time.Sleep(10 * time.Second)
	//m := make(map[string][]string, 0)
	//m["ips"] = []string{"1", "2", "3"}
	////fmt.Printf("%+v\n", m)
	//fs.Execute("dingyz", &goflow.Request{Body: []byte(fmt.Sprintf("you said \"%s\"", "xxxx")), Query: m, Header: m})
	//fmt.Println("已执行")
	//fs.Start()
	//fmt.Println("执行")
	//fs.Execute("myflow1", &goflow.Request{
	//	Body: []byte("hallo"),
	//})
	time.Sleep(10 * time.Second)
}
